package org.itboys.mobile.controller.web;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.entity.mongo.TianJieMember;
import org.itboys.mobile.entity.mysql.TianJieWallet;
import org.itboys.mobile.service.mongo.web.TianJieMemberService;
import org.itboys.mobile.service.mysql.web.TianJieWalletService;
import org.itboys.mobile.util.ResponseMessage;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 作者：江雄
 * 日期：2017年5月10日
 * 描述：天街_金融_用户_controller_web
 */
@Controller
@RequestMapping("/tianjie/web/finance/member")
public class TianJieMemberController extends BaseController{
    @Autowired
    private TianJieMemberService memberService;
    @Autowired
    private TianJieWalletService walletService;
    /**
     * 主页
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/tree/index")
    public String tree_index(HttpServletRequest request,
                        Model model){
        return "/tianjie/member/tree/index";
    }
    /**
     * 主页
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/index")
    public String index(HttpServletRequest request,
                        Model model){
        return "/tianjie/member/index";
    }

    /**
     * 查询积分配置
     * @param request
     * @param response
     */
    @RequestMapping("/select")
    public void select(HttpServletRequest request,
                       HttpServletResponse response){
        try{
            PageResult<TianJieMember> result=memberService.getMember(request);
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
        }catch (Exception e){
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }

    }
    @RequestMapping("/getTree")
    public void getTree(HttpServletRequest request,
                       HttpServletResponse response){
        try{
            List<TianJieMember> members=memberService.getTree(request);
            ResponseMessage.success(members);
        }catch (Exception e){
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }

    }
    /**
     * 单个查询
     * @param request
     * @param response
     * @param id
     */
    @RequestMapping("/findOne")
    public void findOne(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam(value="id",required = true)Long id){
        try {
            TianJieMember member=memberService.findOne(id);
            ResponseMessage.success(member);
        } catch (Exception e) {
            e.printStackTrace();
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/tianjie/web/finance/member/findOne------" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE,msg);

        }
    }
    @RequestMapping("/changeStatus")
    public void changeStatus(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam(value="memberId",required=true)Long memberId,
            @RequestParam(value="status",required=true)Integer status

            ){
        try {
            memberService.changeStatus(memberId,status);
            ResponseMessage.success();
        } catch (Exception e) {
            e.printStackTrace();
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/tianjie/web/finance/member/changeStatus------" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE,msg);
        }
    }
    @RequestMapping("/charge")
    public void charge(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam(value="memberId",required=true)Long memberId,
            @RequestParam(value="chargeAmount",required=true)BigDecimal chargeAmount

    ){
        try {
            walletService.charge(memberId,chargeAmount);
            ResponseMessage.success();
        } catch (Exception e) {
            e.printStackTrace();
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/tianjie/web/finance/member/setCaptain------" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE,msg);
        }
    }



    /**
     * 用户选择器
     * @param request
     * @param isSingle
     * @param param
     * @param model
     * @return
     */
    @RequestMapping("/choose" )
    public  String choose(HttpServletRequest request,
                          @RequestParam(value="isSingle",required=true)Integer isSingle,//是否单选
                          @RequestParam(value="callBack",required=false)String callBack,//回调参数
                          @RequestParam(value="param",required=false)String param,//查询参数
                          Model model){
        model.addAttribute("param",param);
        model.addAttribute("callBack",callBack);
        if(isSingle==null){
            return "/tianjie/member/choose/choose_erro";
        }else if(isSingle==1){
            return "/tianjie/member/choose/choose_single";
        }else if(isSingle==0){
            return "/tianjie/member/choose/choose_muilt";
        }

        return "/tianjie/member/choose/choose_erro";
    }
}
